package org.jsola.hr.helper;

import org.jsola.common.ClassKit;
import org.jsola.common.DateKit;
import org.jsola.hr.constant.CertificateType;
import org.jsola.hr.dto.EmpCertificateAddDTO;
import org.jsola.hr.dto.EmpCertificateUpdateDTO;
import org.jsola.hr.entity.EmpCertificateDO;
import org.jsola.hr.query.EmpCertificateQuery;
import org.jsola.hr.service.IEmpCertificateService;
import org.jsola.hr.vo.EmpCertificateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

import static org.jsola.hr.core.AbstractTest.SITE_ID;



/**
 * @author zhr
 */
@Component
public class EmpCertificateTestHelper extends BaseTestHelper {
    
    @Autowired
    private IEmpCertificateService empCertificateService;

    /**
     * 生成add测试数据
     * @return EmpCertificateAddDTO
     */
    public EmpCertificateAddDTO getEmpCertificateAddDTO(){
        EmpCertificateAddDTO empCertificateAddDTO = new EmpCertificateAddDTO();
        empCertificateAddDTO.setEmpId(getRandomValue("varchar",32));
        empCertificateAddDTO.setType(CertificateType.GNVQS.getValue());
        empCertificateAddDTO.setName(getRandomValue("varchar",255));
        empCertificateAddDTO.setLevel(getRandomValue("varchar",50));
        empCertificateAddDTO.setNumber(getRandomValue("varchar",255));
        empCertificateAddDTO.setStartTime((Date)ClassKit.getObjectValue(getRandomValue("datetime",0),"java.util.Date"));
        empCertificateAddDTO.setEndTime((Date)ClassKit.getObjectValue(getRandomValue("datetime",0),"java.util.Date"));
        empCertificateAddDTO.setRemark(getRandomValue("varchar",255));
        empCertificateAddDTO.setCompanyId(getRandomValue("varchar",32));
        return empCertificateAddDTO;
    }


    /**
     * 生成update测试数据
     * @return EmpCertificateUpdateDTO
     */
    public EmpCertificateUpdateDTO getEmpCertificateUpdateDTO(EmpCertificateVO empCertificateVO){
        return empCertificateService.selectDOById(empCertificateVO.getId(), SITE_ID).to(EmpCertificateUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return EmpCertificateQuery
     */
    public EmpCertificateQuery getEmpCertificateQuery() {
        EmpCertificateQuery empCertificateQuery = new EmpCertificateQuery();
        List<EmpCertificateDO> empCertificateDOList = empCertificateService.list(new EmpCertificateDO(),SITE_ID);
        if (!CollectionUtils.isEmpty(empCertificateDOList)) {
            EmpCertificateDO empCertificateDO = empCertificateDOList.get(0);
            empCertificateQuery = empCertificateDO.to(EmpCertificateQuery.class);
            if (empCertificateDO.getStartTime() != null) {
                empCertificateQuery.setStartTimeStart(DateKit.getBeforeDay(empCertificateDO.getStartTime(),-1));
                empCertificateQuery.setStartTimeEnd(DateKit.getBeforeDay(empCertificateDO.getStartTime(),1));
            }
            if (empCertificateDO.getEndTime() != null) {
                empCertificateQuery.setEndTimeStart(DateKit.getBeforeDay(empCertificateDO.getEndTime(),-1));
                empCertificateQuery.setEndTimeEnd(DateKit.getBeforeDay(empCertificateDO.getEndTime(),1));
            }
            if (empCertificateDO.getGmtCreate() != null) {
                empCertificateQuery.setGmtCreateStart(DateKit.getBeforeDay(empCertificateDO.getGmtCreate(),-1));
                empCertificateQuery.setGmtCreateEnd(DateKit.getBeforeDay(empCertificateDO.getGmtCreate(),1));
            }
            if (empCertificateDO.getGmtModified() != null) {
                empCertificateQuery.setGmtModifiedStart(DateKit.getBeforeDay(empCertificateDO.getGmtModified(),-1));
                empCertificateQuery.setGmtModifiedEnd(DateKit.getBeforeDay(empCertificateDO.getGmtModified(),1));
            }
        }
        return empCertificateQuery;
    }

    /**
     * 保存示例
     * @return EmpCertificateVO
     */
    public EmpCertificateVO saveEmpCertificateExample(){
        EmpCertificateAddDTO addDTO = this.getEmpCertificateAddDTO();
        EmpCertificateVO empCertificateVO = empCertificateService.save(addDTO,buildTokenUser());
        return empCertificateVO;
    }


}

